//
//  ScanLoginConfirmVC.m
//  SchoolHelper
//
//  Created by  任辰东 on 2020/4/7.
//  Copyright © 2020 JSY. All rights reserved.
//

#import "ScanLoginConfirmVC.h"
#import "UIButton+TouchAreaInsets.h"
#import "HomePageApis.h"

@interface ScanLoginConfirmVC ()

@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UILabel *markLabel;
@property (nonatomic, strong) UILabel *tipsLabel;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, copy) NSString *code_no;
@end

@implementation ScanLoginConfirmVC

- (instancetype)initWithCode_no:(NSString *)code_no
{
    self = [super init];
    if (self) {
        _code_no = code_no;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)backBtnAction
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)loginBtnAction
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"code_no"] = self.code_no;
    UserInfoModel *userInfo = [JzyTools getUserInfo];
    params[@"student_id"] = userInfo.student_id;
    WeakSelf
    [self showLoadingHUDWithMessage:@"正在登录..."];
    [HomePageApis LoginCodeRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        [self dismissViewControllerAnimated:YES completion:NULL];
    } FailureBlock:^(NSNumber *code, NSString *msg) {
       
        StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
    
}

- (void)cancelBtnAction
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

-(void)setupViews
{
     self.view.backgroundColor = UIColorFromHex(0xffffff);
     [self.view addSubview:self.backBtn];
     [self.view addSubview:self.logoImageView];
     [self.view addSubview:self.markLabel];
     [self.view addSubview:self.tipsLabel];
     [self.view addSubview:self.loginBtn];
     [self.view addSubview:self.cancelBtn];
    
    [self.backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
}

-(void)setupLayout
{
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.view).offset(KStatusBarHeight+20);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(50);
    }];
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_centerY).offset(-80);
        make.height.mas_equalTo(90);
        make.width.mas_equalTo(120);
    }];
    
    [self.markLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.logoImageView.mas_bottom).offset(15);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
    }];
    
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.markLabel.mas_bottom).offset(5);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.bottom.equalTo(self.view).offset(kBottomOffset-30);
        make.height.mas_equalTo(50);
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.bottom.equalTo(self.cancelBtn.mas_top).offset(-20);
        make.height.mas_equalTo(50);
    }];
}

#pragma mark -- lazyload

-(UIButton *)backBtn
{
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setTitle:@"close" forState:UIControlStateNormal];
        [_backBtn setTitleColor:UIColorFromHex(0x333333) forState:UIControlStateNormal];
        _backBtn.titleLabel.font = UIFontSize(22);
        [_backBtn setTouchAreaInsets:UIEdgeInsetsMake(15, 15, 15, 15)];
    }
    return _backBtn;
}

-(UIImageView *)logoImageView
{
    if (!_logoImageView) {
        _logoImageView = [UIImageView new];
        _logoImageView.image = UIImageWithName(@"scan_computer");
        _logoImageView.contentMode = UIViewContentModeScaleAspectFill;
        _logoImageView.clipsToBounds = YES;
    }
    return _logoImageView;
}

-(UILabel *)markLabel
{
    if (!_markLabel) {
        _markLabel = [UILabel new];
        _markLabel.text = @"教之源学生工作台登录确认";
        _markLabel.font = UIFontSize(18);
        _markLabel.textColor = UIColorFromHex(0x333333);
        _markLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _markLabel;
}

-(UILabel *)tipsLabel
{
    if (!_tipsLabel) {
        _tipsLabel = [UILabel new];
        _tipsLabel.text = @"为保障账户安全,请确认是本人操作";
        _tipsLabel.font = UIFontSize(16);
        _tipsLabel.textColor = UIColorFromHex(0x999999);
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _tipsLabel;
}

-(UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setBackgroundColor:UIColorFromHex(kColorMain)];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:UIColorFromHex(0xffffff) forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = UIFontSize(18);
        _loginBtn.layer.cornerRadius = 25;
        _loginBtn.layer.masksToBounds = YES;
    }
    return _loginBtn;
}

-(UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setBackgroundColor:UIColorFromHex(0xffffff)];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:UIColorFromHex(0x333333) forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = UIFontSize(18);
        _cancelBtn.layer.cornerRadius = 25;
        _cancelBtn.layer.masksToBounds = YES;
        _cancelBtn.layer.borderColor = UIColorFromHex(kColorLine).CGColor;
        _cancelBtn.layer.borderWidth = kOnePixel;
    }
    return _cancelBtn;
}

@end
